﻿using LitJson;
using System.Collections.Generic;

namespace Devil.Persistent
{
    public class GenericPersistentBlock<T> : IPersistentBlock where T : class, IJsonSerializable, new()
    {
        public virtual int Version => 1;
        string mName;
        public string BlockName => mName;

        public virtual EPersistentType BlockType => EPersistentType.GamePlay;

        public GenericPersistentBlock()
        {
            mName = $"PB_{typeof(T).Name}";
        }

        public virtual object CreateInstance(JsonData content)
        {
            return new T();
        }

        public virtual void GatherPersistentDatas(ICollection<object> datas)
        {
            throw new System.NotImplementedException();
        }

        public virtual bool IsInstanceSerializer(object instance)
        {
            return instance is T;
        }

        public virtual void OnDeserializeParallel(object instance, JsonData content)
        {
            if (instance is IJsonSerializable ser)
                ser.OnDeserialize(content);
        }

        public virtual JsonData OnSerializeParallel(object instance)
        {
            if (instance is IJsonSerializable ser)
                return ser.OnSerialize();
            else
                return null;
        }
    }
}
